﻿/**
 * Copyright (c) 2008 Justin Etheredge http://www.codethinked.com
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Microsoft Public License (Ms-PL)
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/ms-pl.html
 * If redistributing this code, this entire header must remain intact.
 * 
 * Some of the code in this application has been taken 
 * from the following sources (all of which are licensed under
 * the Ms-PL):
 * Matt Warren's Blog: http://blogs.msdn.com/mattwar/
 * MSDN: http://msdn2.microsoft.com/en-us/library/bb546158.aspx
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqToSimpleDB
{
  public class SimpleDBItem : IDirtyTrackable, IDeletable, ISaveable
  {
    private string _name = string.Empty;
    private SimpleDBAttributeCollection _attributes = null;
    private bool _isDirty = true;
    private bool _isDeleted = false;
    private bool _saved = false;
    private SimpleDBContext _context = null;

    public string Name
    {
      get { return _name; }
    }

    internal SimpleDBContext Context
    {
      get { return _context; }
      set { _context = value; }
    }

    public SimpleDBAttributeCollection Attributes
    {
      get
      {
        if (_attributes == null)
        {
          _attributes = new SimpleDBAttributeCollection();
          if (this.IsSaved)
          {
            //lazy load our attributes
            this.Context.LoadAttributes(this);
          }
        }
        return _attributes;
      }
    }

    internal SimpleDBItem() { }

    public SimpleDBItem(string name)
      : this(name, false)
    { }

    internal SimpleDBItem(string name, bool createClean)
    {
      if (!createClean)
      {
        ((IDirtyTrackable)this).MarkDirty();
      }
      this._name = name;
    }

    public SimpleDBItem WithAttribute(string name, params string[] values)
    {
      SimpleDBAttribute attribute = new SimpleDBAttribute(name, values);
      this.Attributes.Add(attribute);
      return this;
    }

    public SimpleDBItem WithAttribute(SimpleDBAttribute attribute)
    {
      this.Attributes.Add(attribute);
      return this;
    }

    #region IDirtyTrackable Members

    public bool IsDirty
    {
      get { return _isDirty; }
    }

    void IDirtyTrackable.MarkClean()
    {
      _isDirty = false;
    }

    void IDirtyTrackable.MarkDirty()
    {
      _isDirty = true;
    }

    #endregion

    #region IDeletable Members

    public bool IsDeleted
    {
      get { return _isDeleted; }
    }

    void IDeletable.MarkDeleted()
    {
      _isDeleted = true;
    }

    public void Delete()
    {
      ((IDeletable)this).MarkDeleted();
    }

    #endregion

    #region ISaveable Members

    void ISaveable.MarkSaved()
    {
      _saved = true;
    }

    public bool IsSaved
    {
      get { return _saved; }
    }

    #endregion
  }
}
